/* 
 * Copyright (C) 2018 Igor Maznitsa.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */
package com.igormaznitsa.sciareto.ui.editors.mmeditors;

import com.igormaznitsa.mindmap.ide.commons.SortedTreeModelWrapper;
import com.igormaznitsa.mindmap.model.ExtraTopic;
import com.igormaznitsa.mindmap.model.MindMap;
import com.igormaznitsa.mindmap.model.Topic;
import com.igormaznitsa.mindmap.swing.panel.utils.Utils;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Comparator;
import javax.swing.JTree;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

public final class MindMapTreePanel extends javax.swing.JPanel implements Comparator<Object>{

  private static final long serialVersionUID = 2652308291444091807L;

  private final MindMapTreeCellRenderer cellRenderer = new MindMapTreeCellRenderer();

  private final SortedTreeModelWrapper sortedModel;
  
  public MindMapTreePanel(final MindMap map, final ExtraTopic selectedTopicUid, final boolean expandAll, final ActionListener listener) {
    initComponents();
    this.treeMindMap.setCellRenderer(this.cellRenderer);
    this.treeMindMap.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    if (map != null) {
      this.sortedModel = new SortedTreeModelWrapper(map, this);
      this.treeMindMap.setModel(this.sortedModel);
      if (selectedTopicUid != null) {
        final Topic topic = map.findTopicForLink(selectedTopicUid);
        if (topic != null) {
          this.treeMindMap.setSelectionPath(new TreePath(topic.getPath()));
        }
      }
    }else{
      this.sortedModel = null;
    }

    this.treeMindMap.addMouseListener(new MouseAdapter() {

      @Override
      public void mouseClicked(final MouseEvent e) {
        if (!e.isPopupTrigger() && e.getClickCount() > 1) {
          if (listener != null) {
            listener.actionPerformed(new ActionEvent(this, 0, "doubleClick")); //NOI18N
          }
        }
      }
    });

    
    this.setPreferredSize(new Dimension(450, 400));
    
    if (expandAll){
      Utils.foldUnfoldTree(this.treeMindMap,true);
    }
  }
  
  public JTree getTree() {
    return this.treeMindMap;
  }

  public Topic getSelectedTopic() {
    final TreePath selected = this.treeMindMap.getSelectionPath();
    return selected == null ? null : (Topic) selected.getLastPathComponent();
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    treeScrollPane = new javax.swing.JScrollPane();
    treeMindMap = new javax.swing.JTree();
    toolBar = new javax.swing.JToolBar();
    buttonExpandAll = new javax.swing.JButton();
    buttonCollapseAll = new javax.swing.JButton();
    buttonUnselect = new javax.swing.JButton();

    setLayout(new java.awt.BorderLayout());

    treeScrollPane.setViewportView(treeMindMap);

    add(treeScrollPane, java.awt.BorderLayout.CENTER);

    toolBar.setFloatable(false);
    toolBar.setRollover(true);

    buttonExpandAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/toggle_expand16.png"))); // NOI18N
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("com/igormaznitsa/nbmindmap/i18n/Bundle"); // NOI18N
    buttonExpandAll.setText(bundle.getString("MindMapTreePanel.buttonExpandAll.text_1")); // NOI18N
    buttonExpandAll.setFocusable(false);
    buttonExpandAll.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    buttonExpandAll.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    buttonExpandAll.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonExpandAllActionPerformed(evt);
      }
    });
    toolBar.add(buttonExpandAll);

    buttonCollapseAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/toggle16.png"))); // NOI18N
    buttonCollapseAll.setText(bundle.getString("MindMapTreePanel.buttonCollapseAll.text_1")); // NOI18N
    buttonCollapseAll.setFocusable(false);
    buttonCollapseAll.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    buttonCollapseAll.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    buttonCollapseAll.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonCollapseAllActionPerformed(evt);
      }
    });
    toolBar.add(buttonCollapseAll);

    buttonUnselect.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/select16.png"))); // NOI18N
    buttonUnselect.setText(bundle.getString("MindMapTreePanel.buttonUnselect.text_1")); // NOI18N
    buttonUnselect.setFocusable(false);
    buttonUnselect.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    buttonUnselect.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    buttonUnselect.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        buttonUnselectActionPerformed(evt);
      }
    });
    toolBar.add(buttonUnselect);

    add(toolBar, java.awt.BorderLayout.PAGE_START);
  }// </editor-fold>//GEN-END:initComponents

  private void buttonUnselectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonUnselectActionPerformed
    this.treeMindMap.setSelectionPath(null);
  }//GEN-LAST:event_buttonUnselectActionPerformed

  private void buttonExpandAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonExpandAllActionPerformed
    Utils.foldUnfoldTree(this.treeMindMap, true);
  }//GEN-LAST:event_buttonExpandAllActionPerformed

  private void buttonCollapseAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCollapseAllActionPerformed
    Utils.foldUnfoldTree(this.treeMindMap, false);
  }//GEN-LAST:event_buttonCollapseAllActionPerformed

  public void dispose(){
    if (this.sortedModel != null){
      this.sortedModel.dispose();
    }
  }
  
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton buttonCollapseAll;
  private javax.swing.JButton buttonExpandAll;
  private javax.swing.JButton buttonUnselect;
  private javax.swing.JToolBar toolBar;
  private javax.swing.JTree treeMindMap;
  private javax.swing.JScrollPane treeScrollPane;
  // End of variables declaration//GEN-END:variables

  @Override
  public int compare(final Object o1, final Object o2) {
    return String.CASE_INSENSITIVE_ORDER.compare(String.valueOf(o1), String.valueOf(o2));
  }
}
